# Code for Table H2: Randomization Test

rm(list = ls())

## ---------------------------------------
## Load Packages 
## ---------------------------------------
require('AER')
require('ivpack')
require('dplyr')
require('data.table')

## ---------------------------------------
## Load Data and Functions
## ---------------------------------------
load("../data0812.RData")

directory <- "../functions/"
functions <- list.files(directory)  
loadfunctions <- sapply(functions, FUN = function(x)source(paste0(directory, x)))


## ---------------------------------------
# Construct Instrument
## ---------------------------------------
data0812 <- constructIV(data0812)


## ---------------------------------------
#  Select Last Case Before Election
## ---------------------------------------
data0812 <- lastCase(data0812)


## ---------------------------------------
## Reorder levels to match table
data0812$race <- factor(as.character(data0812$race), levels = c("Black", "White", "Other", "Unknown"))


## Column 1
ss3a <- ivreg(pti ~ age_2012 + I(age_2012^2) + Female + as.factor(race) 
              + as.factor(any_drug_2) +  as.factor(any_violent_2)  
              + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2) 
              + as.factor(court_time1) + as.factor(court_time2)
              + as.factor(prior_offender_2) + as.factor(totOGS2) 
              + as.factor(court_dow) + as.factor(court_shift)
              + vote2008 + as.factor(noteli08) + regis_before,
              data = data0812)

ss3a0 <- ivreg(pti ~ as.factor(court_time1)  + as.factor(court_time2)  + as.factor(court_dow) + as.factor(court_shift) + as.factor(totOGS2),
               data = data0812)


## ---------------------------------------
## Column 2
ss3b <- ivreg(judgeiv ~ age_2012 + I(age_2012^2) + Female + as.factor(race) 
              + as.factor(any_drug_2) +  as.factor(any_violent_2)  
              + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2) 
              + as.factor(court_time1) + as.factor(court_time2) 
              + as.factor(prior_offender_2) + as.factor(totOGS2) 
              + as.factor(court_dow) + as.factor(court_shift)
              + vote2008 + as.factor(noteli08) + regis_before
              , data = data0812)

ss3b0 <- ivreg(judgeiv ~ as.factor(court_time1) + as.factor(court_time2)  + as.factor(court_dow) + as.factor(court_shift) + as.factor(totOGS2)
               , data = data0812)


## Coefficients
t1 <- cbind(round(robust.se(ss3a)[, 1], 5), round(robust.se(ss3b)[, 1], 5))
## Std Errors
t2 <- cbind(round(robust.se(ss3a)[, 2], 5), round(robust.se(ss3b)[, 2], 5))

## Variables to report in the table
keep <- c("age_2012",
          "I(age_2012^2)",
          "Female",
          "as.factor(race)White",
          "as.factor(race)Other",
          "as.factor(race)Unknown",
          "as.factor(any_drug_2)1",
          "as.factor(any_violent_2)1",
          "as.factor(fire_arms_2)1",
          "as.factor(any_rob_2)1",
          "as.factor(any_dui_2)1",
          "as.factor(prior_offender_2)1",
          "vote2008",
          "as.factor(noteli08)1",
          "regis_before")

cat("\nTable H2: Randomization_Test\n")
## Coeffs
coefs <- as.data.frame(t1[rownames(t1) %in% keep, ])
names(coefs) <- c("Pretrial Incarceration Est.", "Magistrate Leniency Est.")
cat("\nPrinting Table H2 estimates...\n")
print(coefs)

## Std Errors
se <- as.data.frame(t2[rownames(t2) %in% keep, ])
names(se) <- c("Pretrial Incarceration SE", "Magistrate Leniency SE")
cat("\nPrinting Table H2 standard errors...\n")
print(se)

## Joint F-test
cat("\nPrinting Table H2, Joint F-Test Pretrial Incarceration...\n")
print(round((anova(ss3a, ss3a0)$F)[2],2))
cat("\nPrinting Table H2, Joint F-Test Magistrate Leniency...\n")
print(round((anova(ss3b, ss3b0)$F)[2], 3))

